module.exports = {
  Query: {
    async accountList(root, args, ctx) {
      const {app} = ctx;
      const result = await app.mysql.query('select * from account');
      console.log(result, '查询数据');
      return result;
    },

    async account(root, args, ctx) {
      const {app} = ctx
      const result = await app.mysql.query("select * from account where id = ?", [args.id])
      return result[0]
    },
  },

  Mutation: {
    async createAccount(root, args, ctx) {
      const {app} = ctx
      console.log("args -->", args);
      const username = args.username;
      const password = args.password;
      const result = await app.mysql.query('insert into account(username, password) values(?,?)', [username, password])
      if (result && result.affectedRows) {
        return '插入成功';
      } else {
        return '插入失败';
      }
    },

    async modifyAccountById(root, args, ctx) {
      const { app } = ctx;
      const id = args.id;
      const username = args.data.username;
      const password = args.data.password;
      const result = await app.mysql.query('update account set username=?, password =?  where id = ?', [username, password, id]);
      if (result && result.affectedRows) {
        return '修改成功';
      } else {
        return '修改失败';
      }
    }
  }

}
